package com.shopping_center.management.controller;

import com.shopping_center.management.common.DataWrapper;
import com.shopping_center.management.common.ResultCode;
import com.shopping_center.management.service.SystemService;
import com.shopping_center.management.system.model.UserInfo;
import com.shopping_center.management.system.model.UserMenu;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author YeFeng
 * @date 2023/3/2
 */
@RestController
@RequestMapping("system")
@CrossOrigin
@Api(tags = "System")
@Slf4j
public class SystemController {

    @Autowired
    private SystemService systemService;

    @PostMapping("login")
    public DataWrapper<?> login(@Validated @RequestBody UserInfo userInfo) {
        UserInfo login = systemService.login(userInfo);
        if (StringUtils.isEmpty(login)) {
            return DataWrapper.errorOf(ResultCode.USER_NOT_EXIST);
        }
        if (!userInfo.getUsername().equals(login.getUsername()) || !userInfo.getPassword().equals(login.getPassword())) {
            return DataWrapper.errorOf(ResultCode.LOGIN_FAILED);
        }
        return DataWrapper.successOf(ResultCode.LOGIN_SUCCESS);
    }

    @PostMapping("selectUserMenu")
    public DataWrapper<?> login(String username) {
        String userId = systemService.selectUserIdByUserName(username);
        List<UserMenu> userMenus = systemService.selectUserMenuByUserId(userId);
        return DataWrapper.successWithData(ResultCode.SELECT_USER_MENU, userMenus);
    }
}
